package beans.factorys.support;

import beans.factorys.config.SingletonBeanRegistry;
import utils.Assert;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by ZhaiGuaili on 2018/7/9.
 */
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
   private final Map<String,Object> singletonMap = new ConcurrentHashMap<String,Object>(64);

    @Override
    public void registrySingleton(String beanName, Object singletonObject) {
        Assert.notNull(beanName,"'beanName' must not be null");
        Object alreadyObject = this.singletonMap.get(beanName);
        if(alreadyObject != null){
            throw new IllegalStateException("Could not register object [" + singletonObject +
                    "] under bean name '" + beanName + "': there is already object [" + alreadyObject + "] bound");
        }
        this.singletonMap.put(beanName,singletonObject);
    }

    @Override
    public Object getSingleton(String beanName) {
        return this.singletonMap.get(beanName);
    }
}
